package com.ruoyi.web.controller.system;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.json.JSON;
import com.google.gson.Gson;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.CarbonIndustryVo;
import com.ruoyi.system.domain.bo.CarbonIndustryBo;
import com.ruoyi.system.service.ICarbonIndustryService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 行业
 *
 * @author ruoyi
 * @date 2023-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/industry")
public class CarbonIndustryController extends BaseController {

    private final ICarbonIndustryService iCarbonIndustryService;

    /**
     * 查询行业列表
     */
    @SaCheckPermission("system:industry:list")
    @GetMapping("/list")
    public TableDataInfo<CarbonIndustryVo> list(CarbonIndustryBo bo, PageQuery pageQuery) {
        return iCarbonIndustryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出行业列表
     */
    @SaCheckPermission("system:industry:export")
    @Log(title = "行业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CarbonIndustryBo bo, HttpServletResponse response) {
        List<CarbonIndustryVo> list = iCarbonIndustryService.queryList(bo);
        ExcelUtil.exportExcel(list, "行业", CarbonIndustryVo.class, response);
    }

    /**
     * 获取行业详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:industry:query")
    @GetMapping("/{id}")
    public R<CarbonIndustryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCarbonIndustryService.queryById(id));
    }

    /**
     * 新增行业
     */
    @SaCheckPermission("system:industry:add")
    @Log(title = "行业", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CarbonIndustryBo bo) {
        return toAjax(iCarbonIndustryService.insertByBo(bo));
    }

    /**
     * 修改行业
     */
    @SaCheckPermission("system:industry:edit")
    @Log(title = "行业", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CarbonIndustryBo bo) {
        return toAjax(iCarbonIndustryService.updateByBo(bo));
    }

    /**
     * 删除行业
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:industry:remove")
    @Log(title = "行业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCarbonIndustryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 导入数据
     * @return
     */

    @GetMapping("/import")
    public R importData(){
        List<CarbonIndustryBo> carbonIndustryBos = parseCarbonIndustryBo();
        iCarbonIndustryService.importData(carbonIndustryBos);
        return R.ok(carbonIndustryBos);
    }

    private static List<CarbonIndustryBo> parseCarbonIndustryBo(){
        String fileName = "C:\\Users\\Administrator\\Desktop\\行业.json";
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine())!= null) {
                sb.append(line);
            }
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
        String str = sb.toString();
        if (StringUtils.isNotBlank(str)){
            List<CarbonIndustryBo> carbonIndustryBos = JsonUtils.parseArray(str, CarbonIndustryBo.class);
            return carbonIndustryBos;
        }
        return null;
    }
}
